package com.asa.customerviewdemos

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup

/**
 * create By：anderson
 * on 2022/2/22
 * desc:
 */
class FlowLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ViewGroup(context, attrs, defStyleAttr) {

    private val list : MutableList<MutableList<View>> = mutableListOf()
    private val listLineHeights = mutableListOf<Int>()
    private var isMeasure = false

    override fun generateLayoutParams(attrs: AttributeSet?): LayoutParams {
        return MarginLayoutParams(context,attrs)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        //根据父容器的MeasureSpec 来得到mode和size
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)

        val widthSize = MeasureSpec.getSize(widthMeasureSpec)
        val heightSize = MeasureSpec.getSize(heightMeasureSpec)

        var childMaxWidth = 0  //最长的那一行的宽度
        var childTotalHeight = 0 //所有行加起来的高度


        var childLineWidth = 0
        var childLineMaxHeight = 0

        if (!isMeasure){
            isMeasure = true
        } else{
            var listView = mutableListOf<View>()
            for (index in 0 until childCount){
                val childView = getChildAt(index)
                measureChildWithMargins(childView,widthMeasureSpec,0,heightMeasureSpec,0)
                val layoutParams = childView.layoutParams as MarginLayoutParams

                val childWidth = childView.measuredWidth + layoutParams.leftMargin + layoutParams.rightMargin
                var childHeight = childView.measuredHeight + layoutParams.topMargin + layoutParams.bottomMargin
                if (childLineWidth + childWidth > widthSize - paddingLeft - paddingRight){
                    //需要换行了
                    childMaxWidth = Math.max(childMaxWidth,childLineWidth)
                    childTotalHeight += childLineMaxHeight
                    listLineHeights.add(childLineMaxHeight)
                    list.add(listView)

                    listView = mutableListOf()
                    childLineWidth = childWidth
                    childLineMaxHeight = childHeight
                    listView.add(childView)
                } else{
                    childLineWidth += childWidth
                    childLineMaxHeight = Math.max(childLineMaxHeight,childHeight)
                    listView.add(childView)
                }
            }
        }

        val measureWidth = if (widthMeasureSpec == MeasureSpec.EXACTLY)  widthSize else childMaxWidth
        val measureHeight = if (heightMeasureSpec == MeasureSpec.EXACTLY) heightSize else childTotalHeight
        setMeasuredDimension(measureWidth,measureHeight)

    }


    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {

        var left =0
        var top = 0
        var right = 0
        var bottom = 0
        var countLeft = paddingLeft  //保存上一个控件的水平边距
        var countTop = paddingTop  //保存上一个控件的垂直边距

        list.forEach{it->
            it.forEach{view->
                val layoutParams = view.layoutParams as MarginLayoutParams
                left = countLeft + layoutParams.leftMargin
                top = countTop + layoutParams.topMargin
                right = left + view.measuredWidth
                bottom = top +  view.measuredHeight + layoutParams.bottomMargin
                view.layout(left,top,right,bottom)

                countLeft += layoutParams.leftMargin + view.measuredWidth + layoutParams.rightMargin
            }

            val indexOf = list.indexOf(it)
            countTop += listLineHeights[indexOf]
            countLeft = paddingLeft

        }
        list.clear()
        listLineHeights.clear()
    }
}